package com.gisocn.onemap.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import butterknife.internal.Utils;
import com.gisocn.onemap.interfaces.OnItemClickListener;
import com.gisocn.onemap.model.TreePoint;
import com.gisocn.onemap.sgzx.R;
import com.gisocn.onemap.utils.FormatSizeUtil;
import com.gisocn.onemap.utils.tree.DensityUtil;
import com.gisocn.onemap.widget.MyProgress;
import java.util.HashMap;
import java.util.List;

/* loaded from: classes2.dex */
public class TreeDownloadAdapter extends BaseAdapter {
    private Context mContext;
    private OnItemClickListener onChildItemClickListener;
    private OnItemClickListener onGroupItemClickListener;
    private List<TreePoint> pointList;
    private HashMap<String, TreePoint> pointMap;

    @Override // android.widget.Adapter
    public long getItemId(int i) {
        return i;
    }

    /* loaded from: classes2.dex */
    public class ViewHolder_ViewBinding implements Unbinder {
        private ViewHolder target;

        public ViewHolder_ViewBinding(ViewHolder viewHolder, View view) {
            this.target = viewHolder;
            viewHolder.layoutGroupAll = (LinearLayout) Utils.findRequiredViewAsType(view, R.id.layout_expand_group_all, "field 'layoutGroupAll'", LinearLayout.class);
            viewHolder.ivGroupLeft = (ImageView) Utils.findRequiredViewAsType(view, R.id.iv_expand_group_arrow, "field 'ivGroupLeft'", ImageView.class);
            viewHolder.tvGroupName = (TextView) Utils.findRequiredViewAsType(view, R.id.tv_expand_group_title, "field 'tvGroupName'", TextView.class);
            viewHolder.tvDownloadAll = (TextView) Utils.findRequiredViewAsType(view, R.id.tv_expand_group_download_all, "field 'tvDownloadAll'", TextView.class);
            viewHolder.tvDownloadNumber = (TextView) Utils.findRequiredViewAsType(view, R.id.tv_expand_group_number, "field 'tvDownloadNumber'", TextView.class);
            viewHolder.layoutChildAll = (LinearLayout) Utils.findRequiredViewAsType(view, R.id.layout_offline_download_item_all, "field 'layoutChildAll'", LinearLayout.class);
            viewHolder.tvTitleChild = (TextView) Utils.findRequiredViewAsType(view, R.id.tv_title, "field 'tvTitleChild'", TextView.class);
            viewHolder.tvSizeChild = (TextView) Utils.findRequiredViewAsType(view, R.id.tv_size, "field 'tvSizeChild'", TextView.class);
            viewHolder.tvProgressChild = (TextView) Utils.findRequiredViewAsType(view, R.id.tv_progress, "field 'tvProgressChild'", TextView.class);
            viewHolder.progressChild = (MyProgress) Utils.findRequiredViewAsType(view, R.id.progress, "field 'progressChild'", MyProgress.class);
            viewHolder.tvStatusChild = (TextView) Utils.findRequiredViewAsType(view, R.id.tv_offline_download_status, "field 'tvStatusChild'", TextView.class);
            viewHolder.btnDownloadChild = (TextView) Utils.findRequiredViewAsType(view, R.id.btn_offline_download, "field 'btnDownloadChild'", TextView.class);
        }

        @Override // butterknife.Unbinder
        public void unbind() {
            ViewHolder viewHolder = this.target;
            if (viewHolder == null) {
                throw new IllegalStateException("Bindings already cleared.");
            }
            this.target = null;
            viewHolder.layoutGroupAll = null;
            viewHolder.ivGroupLeft = null;
            viewHolder.tvGroupName = null;
            viewHolder.tvDownloadAll = null;
            viewHolder.tvDownloadNumber = null;
            viewHolder.layoutChildAll = null;
            viewHolder.tvTitleChild = null;
            viewHolder.tvSizeChild = null;
            viewHolder.tvProgressChild = null;
            viewHolder.progressChild = null;
            viewHolder.tvStatusChild = null;
            viewHolder.btnDownloadChild = null;
        }
    }

    public TreeDownloadAdapter(Context context, List<TreePoint> list, HashMap<String, TreePoint> hashMap) {
        this.pointMap = new HashMap<>();
        this.mContext = context;
        this.pointList = list;
        this.pointMap = hashMap;
    }

    public void setOnGroupItemClickListener(OnItemClickListener onItemClickListener) {
        this.onGroupItemClickListener = onItemClickListener;
    }

    public void setOnChildItemClickListener(OnItemClickListener onItemClickListener) {
        this.onChildItemClickListener = onItemClickListener;
    }

    @Override // android.widget.Adapter
    public int getCount() {
        int i = 0;
        for (TreePoint treePoint : this.pointList) {
            if ("0".equals(treePoint.getPARENTID()) || getItemIsExpand(treePoint.getPARENTID())) {
                i++;
            }
        }
        return i;
    }

    private boolean getItemIsExpand(String str) {
        for (TreePoint treePoint : this.pointList) {
            if (str.equals(treePoint.getID())) {
                return treePoint.isExpand();
            }
        }
        return false;
    }

    @Override // android.widget.Adapter
    public Object getItem(int i) {
        return this.pointList.get(convertPostion(i));
    }

    public int convertPostion(int i) {
        int i2 = 0;
        for (int i3 = 0; i3 < this.pointList.size(); i3++) {
            TreePoint treePoint = this.pointList.get(i3);
            if ("0".equals(treePoint.getPARENTID()) || getItemIsExpand(treePoint.getPARENTID())) {
                i2++;
            }
            if (i == i2 - 1) {
                return i3;
            }
        }
        return 0;
    }

    @Override // android.widget.Adapter
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        View view2;
        int parseInt;
        if (view == null) {
            view2 = LayoutInflater.from(this.mContext).inflate(R.layout.offline_download_item, (ViewGroup) null);
            viewHolder = new ViewHolder(view2);
            view2.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
            view2 = view;
        }
        TreePoint treePoint = (TreePoint) getItem(i);
        if ("0".equals(treePoint.getPARENTID())) {
            viewHolder.layoutGroupAll.setVisibility(0);
            viewHolder.layoutChildAll.setVisibility(8);
            if (!treePoint.isExpand()) {
                viewHolder.ivGroupLeft.setImageResource(R.mipmap.icon_expand_group_right);
            } else {
                viewHolder.ivGroupLeft.setImageResource(R.mipmap.icon_expand_group_down);
            }
            viewHolder.tvDownloadAll.setVisibility(0);
            viewHolder.tvDownloadAll.setEnabled(true);
            viewHolder.tvDownloadAll.setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.adapter.TreeDownloadAdapter.1
                @Override // android.view.View.OnClickListener
                public void onClick(View view3) {
                    if (TreeDownloadAdapter.this.onGroupItemClickListener != null) {
                        TreeDownloadAdapter.this.onGroupItemClickListener.onItemClick(view3, i);
                    }
                }
            });
            String notLoadNumber = treePoint.getNotLoadNumber();
            if (notLoadNumber != null && !TextUtils.isEmpty(notLoadNumber)) {
                if ("0".equals(notLoadNumber)) {
                    viewHolder.tvDownloadAll.setBackgroundResource(R.drawable.bg_black_radius5_storck);
                    viewHolder.tvDownloadAll.setTextColor(ContextCompat.getColor(this.mContext, R.color.colorBgGray));
                    viewHolder.tvDownloadAll.setEnabled(false);
                    viewHolder.tvDownloadAll.setText("全部完成");
                    viewHolder.tvDownloadNumber.setVisibility(4);
                } else {
                    viewHolder.tvDownloadAll.setBackgroundResource(R.drawable.bg_primary_radius5_storck);
                    viewHolder.tvDownloadAll.setTextColor(ContextCompat.getColor(this.mContext, R.color.colorPrimary));
                    viewHolder.tvDownloadAll.setEnabled(true);
                    viewHolder.tvDownloadAll.setText("下载全部");
                    viewHolder.tvDownloadNumber.setVisibility(0);
                }
            }
            viewHolder.tvDownloadNumber.setText(notLoadNumber);
            viewHolder.tvGroupName.setCompoundDrawablePadding(DensityUtil.dip2px(this.mContext, 10.0f));
            viewHolder.tvGroupName.setText(treePoint.getNNAME());
        } else {
            viewHolder.layoutGroupAll.setVisibility(8);
            viewHolder.layoutChildAll.setVisibility(0);
            viewHolder.tvTitleChild.setCompoundDrawablePadding(DensityUtil.dip2px(this.mContext, 10.0f));
            viewHolder.tvTitleChild.setText(treePoint.getNNAME());
            TextView textView = viewHolder.tvSizeChild;
            textView.setText(FormatSizeUtil.getFormatSize(Double.parseDouble(treePoint.getSize())) + "");
            viewHolder.btnDownloadChild.setEnabled(true);
            viewHolder.btnDownloadChild.setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.adapter.TreeDownloadAdapter.2
                @Override // android.view.View.OnClickListener
                public void onClick(View view3) {
                    if (TreeDownloadAdapter.this.onChildItemClickListener != null) {
                        TreeDownloadAdapter.this.onChildItemClickListener.onItemClick(view3, i);
                    }
                }
            });
            viewHolder.tvProgressChild.setText("");
            viewHolder.btnDownloadChild.setVisibility(0);
            viewHolder.progressChild.setVisibility(8);
            viewHolder.btnDownloadChild.setBackgroundResource(R.drawable.bg_primary_radius5_storck);
            viewHolder.btnDownloadChild.setTextColor(ContextCompat.getColor(this.mContext, R.color.colorPrimary));
            if (treePoint.getVersionDesc().equals("未下载")) {
                viewHolder.btnDownloadChild.setText("下载");
            } else if (treePoint.getVersionDesc().equals("已是最新版本")) {
                viewHolder.tvStatusChild.setText("已是最新版本");
                viewHolder.tvStatusChild.setTextColor(ContextCompat.getColor(this.mContext, R.color.colorBlack));
                viewHolder.btnDownloadChild.setText("已完成");
                viewHolder.btnDownloadChild.setEnabled(false);
                viewHolder.btnDownloadChild.setBackgroundResource(R.drawable.bg_black_radius5_storck);
                viewHolder.btnDownloadChild.setTextColor(ContextCompat.getColor(this.mContext, R.color.colorBgGray));
            } else if (treePoint.getVersionDesc().equals("有更新版本")) {
                viewHolder.btnDownloadChild.setText("更新");
                viewHolder.tvStatusChild.setText("有更新版本");
            } else if (treePoint.getVersionDesc().equals("未完成")) {
                viewHolder.btnDownloadChild.setText("下载");
                viewHolder.tvProgressChild.setText("");
                viewHolder.progressChild.setVisibility(0);
                viewHolder.progressChild.setProgress(0);
            }
            TextView textView2 = viewHolder.tvStatusChild;
            textView2.setText(treePoint.getVersionDesc() + "");
            viewHolder.progressChild.setProgress(0);
            String currentLength = treePoint.getCurrentLength();
            viewHolder.btnDownloadChild.setVisibility(0);
            viewHolder.progressChild.setVisibility(4);
            if (currentLength != null && currentLength.length() > 0 && (parseInt = Integer.parseInt(currentLength)) > 0 && parseInt < 100) {
                viewHolder.tvStatusChild.setText("下载中...");
                viewHolder.tvStatusChild.setTextColor(ContextCompat.getColor(this.mContext, R.color.colorBlue));
                viewHolder.btnDownloadChild.setVisibility(4);
                viewHolder.progressChild.setVisibility(0);
                viewHolder.progressChild.setProgress(Integer.parseInt(treePoint.getCurrentLength()));
            }
        }
        return view2;
    }

    public void onItemClick(int i) {
        TreePoint treePoint = (TreePoint) getItem(i);
        if (!"1".equals(treePoint.getISLEAF())) {
            if (treePoint.isExpand()) {
                for (TreePoint treePoint2 : this.pointList) {
                    if (treePoint2.getPARENTID().equals(treePoint.getID()) && "0".equals(treePoint.getISLEAF())) {
                        treePoint2.setExpand(false);
                    }
                }
                treePoint.setExpand(false);
            } else {
                treePoint.setExpand(true);
            }
        }
        notifyDataSetChanged();
    }

    private String getSubmitResult(TreePoint treePoint) {
        StringBuilder sb = new StringBuilder();
        addResult(treePoint, sb);
        String sb2 = sb.toString();
        return sb2.endsWith("-") ? sb2.substring(0, sb2.length() - 1) : sb2;
    }

    private void addResult(TreePoint treePoint, StringBuilder sb) {
        if (treePoint == null || sb == null) {
            return;
        }
        sb.insert(0, treePoint.getNNAME() + "-");
        if ("0".equals(treePoint.getPARENTID())) {
            return;
        }
        addResult(this.pointMap.get(treePoint.getPARENTID()), sb);
    }

    /* loaded from: classes2.dex */
    public static class ViewHolder {
        @BindView(R.id.btn_offline_download)
        TextView btnDownloadChild;
        @BindView(R.id.iv_expand_group_arrow)
        ImageView ivGroupLeft;
        @BindView(R.id.layout_offline_download_item_all)
        LinearLayout layoutChildAll;
        @BindView(R.id.layout_expand_group_all)
        LinearLayout layoutGroupAll;
        @BindView(R.id.progress)
        MyProgress progressChild;
        @BindView(R.id.tv_expand_group_download_all)
        TextView tvDownloadAll;
        @BindView(R.id.tv_expand_group_number)
        TextView tvDownloadNumber;
        @BindView(R.id.tv_expand_group_title)
        TextView tvGroupName;
        @BindView(R.id.tv_progress)
        TextView tvProgressChild;
        @BindView(R.id.tv_size)
        TextView tvSizeChild;
        @BindView(R.id.tv_offline_download_status)
        TextView tvStatusChild;
        @BindView(R.id.tv_title)
        TextView tvTitleChild;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
